//
//  ScrollImgViewController.m


#import "ScrollImgViewController.h"
#import "ScrollImgView.h"
#import "UIImageView+WebCache.h"


@interface ScrollImgViewController ()<UIScrollViewDelegate>

/** 自定义view*/
@property (nonatomic,strong) ScrollImgView *sv;

@end

@implementation ScrollImgViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        //在这里控制轮播图大小
        //imageCount为轮播图的图片个数
        self.sv = [[ScrollImgView alloc] initWithFrame:CGRectMake(10, 10, 300, 160) imageCount:6];
        self.imgArray = [NSMutableArray array];
    }
    return self;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view = self.sv;
    
    //添加代理
    self.sv.scroll.delegate = self;
    [self.sv.page addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
    //设置数据
    [self p_updateImageData];
    //计时器
    [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(timeAction) userInfo:nil repeats:YES];
}

//带动画效果自动轮播
- (void)timeAction{
    __block typeof(self) weakSelf = self;
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    [UIView animateWithDuration:0.3 animations:^{
        CGPoint currentPoint = weakSelf.sv.scroll.contentOffset;
        currentPoint.x += CGRectGetWidth(weakSelf.sv.frame);
        weakSelf.sv.scroll.contentOffset = currentPoint;
    }];
    
    [self scrollViewDidEndDecelerating:self.sv.scroll];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark pageAction 响应事件方法
- (void)pageAction:(UIPageControl *)sender{
    
    self.sv.scroll.contentOffset = CGPointMake(CGRectGetWidth(self.sv.frame)*sender.currentPage, CGRectGetMinY(self.sv.frame));
    
    
}

#pragma mark 添加图片
- (void)p_updateImageData{
    
    
    for (int i = 0; i < self.sv.imgCount; i++) {

        UIImageView *imV = (UIImageView *)[self.sv.scroll viewWithTag:100+i];

        NSString *urlStr = [NSString stringWithFormat:@"http://pic.ecook.cn/web/%@.jpg",self.imgArray[i]];
        
        [imV setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:[UIImage imageNamed:@"3.jpg"]];
    }
    
    
}

#pragma mark -------scrollView代理方法
//减速完成
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    int index = scrollView.contentOffset.x / self.sv.frame.size.width;
    //判断最后一张图的情况
    if (index == self.sv.imgCount-1) {
        //scrollView立即回退到底一张
        scrollView.contentOffset = CGPointMake(CGRectGetWidth(self.sv.bounds), CGRectGetMinY(self.sv.bounds));
        self.sv.page.currentPage = 0;
    }
    //判断第一张图的情况
    else if(index == 0){
        //scrollView立即回退到最后一张
        scrollView.contentOffset = CGPointMake(CGRectGetWidth(self.sv.bounds)*(self.sv.imgCount-2), CGRectGetMinY(self.sv.bounds));
        self.sv.page.currentPage = self.sv.page.numberOfPages - 1;
    }else{
        self.sv.page.currentPage = index-1;
    }
    
}




@end
